package com.handpay.nocardpay.baselibrary.utils;

import android.content.Context;
import android.text.TextUtils;

import com.handpay.nocardpay.baselibrary.NoCardPayApplication;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;

import java.io.Serializable;

public class AccountUtils {
    private static final String RMS_ACCOUNT_KEY = "rms_account";
    private static final String RMS_ACCOUNT_PWD = "rms_pwd";
    private static final String RMS_SELECTED_CHECKBOX = "rms_check";
    public static final int MINLEN = 6;
    public static final int MAXLEN = 20;

    public static synchronized void saveAccount(Context context, String acc, String phone, AccountStatus status, String failDes, String merchantCode, String userId) {
        if (null == ((NoCardPayApplication) context.getApplicationContext()).getAccount()) {
            HPLog.i(HPLog.TAG_RESULT, "NoCardPayApplication Account new object");
            Account account = new Account();
            ((NoCardPayApplication) context.getApplicationContext()).setAccount(account);
        }
        ((NoCardPayApplication) context.getApplicationContext()).getAccount().account = acc;
        ((NoCardPayApplication) context.getApplicationContext()).getAccount().phone = phone;
        ((NoCardPayApplication) context.getApplicationContext()).getAccount().status = status;
        ((NoCardPayApplication) context.getApplicationContext()).getAccount().failDes = failDes;
        ((NoCardPayApplication) context.getApplicationContext()).getAccount().merchantCode = merchantCode;
        ((NoCardPayApplication) context.getApplicationContext()).getAccount().userId = userId;
    }

    public static synchronized void setWDPermission(Context context, String wdPermission) {
        // 需要判断长度为2么？这样写死了不太好
        if (wdPermission != null && wdPermission.trim().length() > 0) {
            AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
            if (account != null)
                account.wdPermission = wdPermission;
        }
    }

    public static synchronized void saveUserName(Context context, String name) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        if (account != null) {
            account.username = name;
        }
    }

    public static String getUserName(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        if (account != null) {
            return account.username;
        }
        return null;
    }

    public static synchronized void saveUsercertNo(Context context, String certno) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        if (account != null) {
            account.usercertNo = certno;
        }
    }

    public static String getUsercertNo(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        if (account != null) {
            return account.usercertNo;
        }
        return null;
    }

    public static synchronized void saveUserCardNo(Context context, String cardno) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        if (account != null) {
            account.usercardNo = cardno;
        }
    }

    public static String getUserCardNo(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        if (account != null) {
            return account.usercardNo;
        }
        return null;
    }

    public static synchronized void savePasswordStatus(Context context, int status) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        account.passwordStatus = status;
    }

    public static int getUserPasswordStatus(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        return account.passwordStatus;
    }

    /**
     * 按钮是否可见 按钮是否可用 0 0 不可见 0 1 不可见 1 0 可见不可用 1 1 可见可用
     */
    // 是否可见
    public static synchronized boolean isWDVisible(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        String wdPermission = account == null ? "00" : account.wdPermission;
        // 后台没有传，当00
        if (TextUtils.isEmpty(wdPermission)) {
            wdPermission = "00";
        }
        return '1' == wdPermission.charAt(0);
    }

    // 是否可用
    public static synchronized boolean isWDUsable(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        String wdPermission = account == null ? "00" : account.wdPermission;
        // 后台没有传，当00
        if (TextUtils.isEmpty(wdPermission)) {
            wdPermission = "00";
        }
        return wdPermission.length() >= 2 && '1' == wdPermission.charAt(1);
    }

    public static synchronized String getShopName(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        return null == account ? null : account.shopName;
    }

    public static synchronized void setShopName(Context context, String name) {
        if (name != null && name.trim().length() > 0) {
            AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
            account.shopName = name.trim();
        }
    }

    /**
     * 用户账户名，而非资质审核的姓名
     *
     * @return
     */
    public static synchronized String getAccountName(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        return null == account ? null : account.account;
    }

    /***
     * 获取userId
     * @param context
     * @return
     */
    public static synchronized String getUserId(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        return null == account ? null : account.userId;
    }

    public static synchronized void setAccountName(Context context, String name) {
        if (name != null && name.trim().length() > 0) {
            AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
            account.account = name;
        }
    }

    public static synchronized String getMerchantCode(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        String merchantCode = (ClientEngine.getInstance().CHANNEL.equals("HPZZT") ? "hpayZZT" : ClientEngine.getInstance().CHANNEL) + "@" + AccountUtils.getAccountName(context);
        return null == account.merchantCode ? merchantCode : account.merchantCode;
    }


    /**
     * 是否打开T0资质信息拍照
     */
    public static synchronized void setAccountT0Switch(Context context, String isOpenT0Switch) {
        if (isOpenT0Switch != null && !TextUtils.isEmpty(isOpenT0Switch)) {
            AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
            account.isOpenT0switch = isOpenT0Switch;
        }
    }

    /**
     * 是否打开T0资质信息拍照
     *
     * @return
     */
    public static synchronized String getAccountT0Switch(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        return null != account && account.isOpenT0switch != null ? account.isOpenT0switch : null;
    }

    /***
     * 修改手机号或添加手机号
     *
     * @param strPhone
     */
    public static synchronized void setAccountPhone(Context context, String strPhone) {
        if (strPhone != null && strPhone.trim().length() > 0) {
            AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
            account.phone = strPhone;
        }
    }

    /**
     * 设置资质审核状态
     *
     * @param status
     */
    public static synchronized void setAccountStatus(Context context, AccountStatus status) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        account.status = status;
    }

    public static synchronized String getAccountPhone(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();

        return null == account ? null : account.phone;
    }

    public static synchronized AccountStatus getAccountStatus(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        return null == account ? null : account.status;
    }

    public static synchronized String getAccountFailDes(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        return null == account ? null : account.failDes;
    }

    public static synchronized void setAccountFailDes(Context context, String failDescription) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        account.failDes = failDescription;
    }

    /**
     * 设置用户证件状态
     *
     * @param status
     */
    public static synchronized void setCertNoExporyStatus(Context context, String status) {
        if (!TextUtils.isEmpty(status)) {
            AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
            account.certNoExporyFlag = status;
        }
    }

    /**
     * 获取用户证件状态
     *
     * @param context
     * @return 默认为-1 无相关状态
     */
    public static synchronized String getCertNoExporyStatus(Context context) {
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        return account != null ? account.certNoExporyFlag : "-1";
    }

    public static AccountStatus convertStatus(String status) {
        AccountStatus as = null;
        if ("00".equals(status)) {
            as = AccountStatus.SUCCESS;
        } else if ("01".equals(status)) {
            as = AccountStatus.NOUPLOAD;
        } else if ("02".equals(status)) {
            as = AccountStatus.CHECKING;
        } else if ("03".equals(status)) {
            as = AccountStatus.FAIL;
        }
        return as;
    }

    /***
     * 清除账户信息
     */
    public static void clearAccount(Context context) {
        HPLog.i(HPLog.YL_TAG, "ClearAccount:");
        AccountUtils.Account account = ((NoCardPayApplication) context.getApplicationContext()).getAccount();
        if (account != null)
            ((NoCardPayApplication) context.getApplicationContext()).setAccount(null);
    }

    public static class Account implements Serializable {
        /**
         * 证件无相关信息
         */
        public static final String CERTNO_NOSTATUS = "-1";
        /**
         * 补件审核通过
         */
        public static final String CERTNO_SUCCESS = "00";
        /**
         * 待补件
         */
        public static final String CERTNO_NOSUPPLEMENT = "01";
        /**
         * 补件审核中
         */
        public static final String CERTNO_CHECKING = "02";
        /**
         * 补件审核未通过
         */
        public static final String CERTNO_FAIL = "03";
        AccountStatus status;
        String account;
        String phone;
        String failDes;
        String shopName;
        String wdPermission;
        String merchantCode;
        String isOpenT0switch;
        String username;
        String usercertNo;
        String usercardNo;
        String userId;
        int passwordStatus;
        /**
         * 证件当前状态
         */
        String certNoExporyFlag = "-1";

        public AccountStatus getStatus() {
            return this.status;
        }

        public String getAccount() {
            return this.account;
        }

        public String getPhone() {
            return this.phone;
        }

        public String getFailDes() {
            return this.failDes;
        }

        public String getShopName() {
            return this.shopName;
        }

        public String getwdPermission() {
            return this.wdPermission;
        }

        public String getMerchantCode() {
            return this.merchantCode;
        }

        public String getIsOpenT0switch() {
            return this.isOpenT0switch;
        }

        public String getUsername() {
            return this.username;
        }

        public String getUsercertNo() {
            return this.usercertNo;
        }

        public String getUsercardNo() {
            return this.usercardNo;
        }

        public String getUserId() {
            return this.userId;
        }

        public int getPasswordStatus() {
            return this.passwordStatus;
        }

        public String getCertNoExporyFlag() {
            return this.certNoExporyFlag;
        }

        @Override
        public String toString() {
            return "account:" + account + ",phone:" + phone + ",merchantCode:" + merchantCode + ",username:" + username + ",passwordStatus:" + passwordStatus;
        }
    }

    public enum AccountStatus {
        // 审核成功
        SUCCESS,
        // 提交审核
        CHECKING,
        // 审核失败
        FAIL,
        // 注册，但未上传
        NOUPLOAD,
    }


    public static boolean isAccountLegal(String account) {
        boolean result = CommonUtils.isNumAndChar(account);
        if (result) {
            return account.length() >= MINLEN && account.length() <= MAXLEN;
        }
        return false;
    }

    /**
     *  判断密码是否合法，长度可自由控制
     * @param account
     * @param length
     * @return
     */
    public static boolean isAccountLegal(String account, int length) {
        boolean result = CommonUtils.isNumAndChar(account);
        if (result) {
            return account.length() >= length && account.length() <= MAXLEN;
        }
        return false;
    }

    public static boolean isPwdLegal(String pwd) {
        // 两个要求是一样的，所以暂时直接调用
        return isAccountLegal(pwd);
    }

    /**
     * 用户保存登录成功的用户名
     *
     * @param account
     */
    public static boolean saveRMSAccountName(String account) {
        if (!TextUtils.isEmpty(account)) {
            ClientEngine.getInstance().saveRMSData(RMS_ACCOUNT_KEY, account);
            return true;
        }
        return false;
    }

    public static void removeRMSPWDData() {
        ClientEngine.getInstance().removedData(RMS_ACCOUNT_PWD);
    }

    /**
     * 保存用户登入密码
     *
     * @param pwd
     * @return
     */
    public static boolean saveRMSAccountPWD(String pwd) {
        if (!TextUtils.isEmpty(pwd)) {
            ClientEngine.getInstance().saveRMSData(RMS_ACCOUNT_PWD, pwd);
            return true;
        }
        return false;
    }

    /**
     * 保存是否记住密码
     *
     * @param isCheck
     */
    public static void saveSelectRemenberCheck(boolean isCheck) {
        // TODO: 2017/8/8 保存用户信息需要修改用@{link PreferenceManager}
//        ClientEngine.getInstance().saveRMSData(RMS_SELECTED_CHECKBOX, isCheck);
    }

    public static Object getSelectRemenberCheck() {
        Object obj = ClientEngine.getInstance().getRMSData(RMS_SELECTED_CHECKBOX);
        return obj != null ? obj : null;
    }

    public static String getRMSAccountPwd() {
        Object c = ClientEngine.getInstance().getRMSData(RMS_ACCOUNT_PWD);
        if (null != c) {
            return (String) c;
        }
        return null;
    }

    public static String getRMSAccountName() {
        Object c = ClientEngine.getInstance().getRMSData(RMS_ACCOUNT_KEY);
        if (null != c) {
            return (String) c;
        }
        return null;
    }

    public static boolean isNeelUpload(Context context) {

        return AccountStatus.FAIL.equals(getAccountStatus(context));
    }

}
